﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Runtime.Serialization;

namespace CMTVision.WidgetWeb
{
    public class TestClass
    {
        private string TestField;
    }
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        { 

            if (!IsPostBack)
            {
                BindData();
            }
            themeLink.Href = ThemeCssFile;
            colorLink.Href = ColorCssFile;
        }
        protected override void OnPreInit(EventArgs e)
        {
            base.OnPreInit(e);

            TemplateContainer.TemplateContainerID = Convert.ToInt64(Request["id"] ?? "1");
            TemplateContainer.IsInEditMode = true;
            //CurrentTheme = "default";
            //CurrentColor = "default";
        }

        public string ThemeCssFile
        {
            get
            {
                return "Designs/Themes/" + CurrentTheme + "/theme.css";
            }
        }

        public string ColorCssFile
        {
            get
            {
                return "Designs/Themes/" + CurrentTheme + "/Colors/" + CurrentColor ;
            }
        }

        protected string CurrentTheme
        {
            get
            {
                return (ViewState["CurrentTheme"] ?? String.Empty).ToString();
            }
            set
            {
                ViewState["CurrentTheme"] = value;
            }
        }
        protected string CurrentColor
        {
            get
            {
                return (ViewState["CurrentColor"] ?? String.Empty).ToString();
            }
            set
            {
                ;
                ViewState["CurrentColor"] = value;
            }
        }

        protected void ThemeDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            CurrentTheme = ThemeDropDownList.SelectedValue;
            string currentThemeFolder = Server.MapPath("~/Designs/Themes/" + ThemeDropDownList.SelectedValue);
            string colorsFolder = Path.Combine(currentThemeFolder, "Colors");
            DirectoryInfo colorsInfo = new DirectoryInfo(colorsFolder);
            FileInfo[] allColorsInfos = colorsInfo.GetFiles("*.css");

            List<string> allColors = new List<string>();
            foreach (FileInfo colorInfo in allColorsInfos)
            {
                allColors.Add(colorInfo.Name);
            }
            ColorDropDownList.DataSource = allColors;
            ColorDropDownList.DataBind();
            ColorDropDownList.SelectedIndex = 0;
            ColorDropDownList_SelectedIndexChanged(ColorDropDownList, EventArgs.Empty);
            //UpdatePanel1.Update();
        }

        protected void ColorDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            CurrentTheme = ThemeDropDownList.SelectedValue;
            CurrentColor = ColorDropDownList.SelectedValue;

            themeLink.Href = ThemeCssFile;
            colorLink.Href = ColorCssFile;
            //UpdatePanel1.Update();
        }

        protected void BindData()
        {
            string themesFolder = Server.MapPath("~/Designs/Themes");
            DirectoryInfo dInfo = new DirectoryInfo(themesFolder);
            DirectoryInfo[] allThemesInfos=dInfo.GetDirectories();
            List<string> allThemes = new List<string>();
            foreach (DirectoryInfo childInfo in allThemesInfos)
            {
                allThemes.Add(childInfo.Name);
            }
            ThemeDropDownList.DataSource = allThemes;
            ThemeDropDownList.DataBind();
            ThemeDropDownList.SelectedIndex = 0;

            ThemeDropDownList_SelectedIndexChanged(ThemeDropDownList, EventArgs.Empty);

            themeLink.Href = ThemeCssFile;
            colorLink.Href = ColorCssFile;
             

        }

    }
}